package com.zhaopin.utils;

import com.zhaopin.bean.ParamQueryDetailVO;
import com.zhaopin.bean.ParamVO;
import org.apache.commons.lang3.StringUtils;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @create 2020-01-15 10:57
 */
public class YmlUtils {

    public static void main(String[] args) throws IOException {
        getParamYaml("E:\\gitrepo\\java-s-gateway\\server\\env\\preDocker\\prapi\\param-pr-recommend-base.yml");
    }


    public static void getParamYaml(String filePath) throws IOException {
        StringBuilder strq = new StringBuilder();
        StringBuilder strfq = new StringBuilder();

        File file = new File(filePath);
        ParamVO paramVO = new ParamVO();
        Yaml yaml = new Yaml();
        FileInputStream fi = new FileInputStream(file);
        paramVO = yaml.loadAs(fi, ParamVO.class);
        fi.close();

        // 进行数据转化,转化q的数据:
        List<ParamQueryDetailVO> listq = paramVO.getQ();
        if (null != listq) {
            for (int i = 0; i < listq.size(); i++) {
                ParamQueryDetailVO paramQueryDetailVO = listq.get(i);
                if (StringUtils.isNotBlank(paramQueryDetailVO.getQuery()) && StringUtils.isNotBlank(paramQueryDetailVO.getField())) {
                    strq.append("customQuery(" + "\""
                            + paramQueryDetailVO.getQuery().replace("%s", "\\${" + paramQueryDetailVO.getField() + "}")
                            + "\"" + "),\r\n");
                    continue;
                }
                // array
                if (StringUtils.isNotBlank(paramQueryDetailVO.getMapper())
                        && StringUtils.isNotBlank(paramQueryDetailVO.getField())
                        && StringUtils.isNotBlank(paramQueryDetailVO.getType())
                        && "array".equals(paramQueryDetailVO.getType())
                        && StringUtils.isBlank(paramQueryDetailVO.getOperate())) {
                    strq.append("match().field(\"" + paramQueryDetailVO.getField() + "\").mapper(\"" + paramQueryDetailVO.getMapper() + "\"),\r\n");
                    continue;
                }
                //range
                if (StringUtils.isNotBlank(paramQueryDetailVO.getMapper())
                        && StringUtils.isNotBlank(paramQueryDetailVO.getField())
                        && StringUtils.isNotBlank(paramQueryDetailVO.getType())
                        && "range".equals(paramQueryDetailVO.getType())) {
                    strq.append("range().field(\"" + paramQueryDetailVO.getField() + "\").mapper(\"" + paramQueryDetailVO.getMapper() + "\"),\r\n");
                    continue;
                }
                // not and
                if (StringUtils.isNotBlank(paramQueryDetailVO.getMapper())
                        && StringUtils.isNotBlank(paramQueryDetailVO.getField())
                        && StringUtils.isNotBlank(paramQueryDetailVO.getType())
                        && "array".equals(paramQueryDetailVO.getType())
                        && StringUtils.isNotBlank(paramQueryDetailVO.getOperate())
                        && "not and".equals(paramQueryDetailVO.getOperate())) {
                    strq.append("match().field(\"" + paramQueryDetailVO.getField() + "\").mapper(\"" + paramQueryDetailVO.getMapper() + "\").excludeOpNot().mapperOpAnd(),\r\n");
                    continue;
                }
                //not or
                if (StringUtils.isNotBlank(paramQueryDetailVO.getMapper())
                        && StringUtils.isNotBlank(paramQueryDetailVO.getField())
                        && StringUtils.isNotBlank(paramQueryDetailVO.getType())
                        && "array".equals(paramQueryDetailVO.getType())
                        && StringUtils.isNotBlank(paramQueryDetailVO.getOperate())
                        && "not or".equals(paramQueryDetailVO.getOperate())) {
                    strq.append("match().field(\"" + paramQueryDetailVO.getField() + "\").mapper(\"" + paramQueryDetailVO.getMapper() + "\").excludeOpNot().mapperOpOr(),\r\n");
                    continue;
                }

            }
        }
        //进行 fq的数据优化q
        List<ParamQueryDetailVO> listFq = paramVO.getFq();
        for (int i = 0; i < listFq.size(); i++) {
            ParamQueryDetailVO paramQueryDetailVO = listFq.get(i);

            //array
            if (StringUtils.isNotBlank(paramQueryDetailVO.getMapper())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getField())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getType())
                    && "array".equals(paramQueryDetailVO.getType())
                    && StringUtils.isBlank(paramQueryDetailVO.getOperate())) {
                strfq.append("match().field(\"" + paramQueryDetailVO.getField() + "\").mapper(\"" + paramQueryDetailVO.getMapper() + "\"),\r\n");
                continue;
            }

            // and
            if (StringUtils.isNotBlank(paramQueryDetailVO.getMapper())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getField())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getType())
                    && "array".equals(paramQueryDetailVO.getType())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getOperate())
                    && "and".equals(paramQueryDetailVO.getOperate())) {
                strfq.append("match().field(\"" + paramQueryDetailVO.getField() + "\").mapper(\"" + paramQueryDetailVO.getMapper() + "\").mapperOpAnd(),\r\n");
                continue;
            }

            //not and
            if (StringUtils.isNotBlank(paramQueryDetailVO.getMapper())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getField())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getType())
                    && "array".equals(paramQueryDetailVO.getType())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getOperate())
                    && "not and".equals(paramQueryDetailVO.getOperate())) {
                strfq.append("match().field(\"" + paramQueryDetailVO.getField() + "\").mapper(\"" + paramQueryDetailVO.getMapper() + "\").excludeOpNot().mapperOpAnd(),\r\n");
                continue;
            }
            //not or
            if (StringUtils.isNotBlank(paramQueryDetailVO.getMapper())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getField())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getType())
                    && "array".equals(paramQueryDetailVO.getType())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getOperate())
                    && "not or".equals(paramQueryDetailVO.getOperate())) {
                strfq.append("match().field(\"" + paramQueryDetailVO.getField() + "\").mapper(\"" + paramQueryDetailVO.getMapper() + "\").excludeOpNot().mapperOpOr(),\r\n");
                continue;
            }

            // field + query
            if (StringUtils.isNotBlank(paramQueryDetailVO.getQuery()) && StringUtils.isNotBlank(paramQueryDetailVO.getField())) {
                strfq.append("customQuery(" + "\""
                        + paramQueryDetailVO.getQuery().replace("%s", "\\${" + paramQueryDetailVO.getField() + "}")
                        + "\"" + "),\r\n");
                continue;
            }

            // field + query
            if (StringUtils.isNotBlank(paramQueryDetailVO.getQuery()) && StringUtils.isBlank(paramQueryDetailVO.getField())) {
                strfq.append("customQuery(" + "\"" + paramQueryDetailVO.getQuery() + "\"" + "),\r\n");
                continue;
            }
            //range
            if (StringUtils.isNotBlank(paramQueryDetailVO.getMapper())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getField())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getType())
                    && "range".equals(paramQueryDetailVO.getType())
                    && StringUtils.isBlank(paramQueryDetailVO.getOperate())) {
                strfq.append("range().field(\"" + paramQueryDetailVO.getField() + "\").mapper(\"" + paramQueryDetailVO.getMapper() + "\"),\r\n");
                continue;
            }

            //coordinate
            if (StringUtils.isNotBlank(paramQueryDetailVO.getMapper())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getField())
                    && StringUtils.isNotBlank(paramQueryDetailVO.getType())
                    && "coordinate".equals(paramQueryDetailVO.getType())
                    && StringUtils.isBlank(paramQueryDetailVO.getOperate())) {
                strfq.append("coordinate().field(\"" + paramQueryDetailVO.getField() + "\").mapper(\"" + paramQueryDetailVO.getMapper() + "\"),\r\n");
                continue;
            }
        }

        System.out.println("q:\r\n" + strq.toString());
        System.out.println("----------------------");
        System.out.println("fq:\r\n" + strfq);
    }

}
